<template>
  <div class="table_data">
    <EtypeHeader/>
    <el-table :data="tunnelInfo" :cell-style="{'text-align':'center'}" :header-cell-style="{background: '#f8f8f9',textAlign: 'center'}">
      <el-table-column width="100" show-overflow-tooltip prop="eId" label="序号"></el-table-column>
      <el-table-column width="200" show-overflow-tooltip prop="eName" label="开挖方式"></el-table-column>
      <el-table-column show-overflow-tooltip label="操作">
        <template slot-scope="scope">
          <el-button size="mini" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
        <el-pagination
            @current-change="handleCurrentChange"
            layout="total, sizes, prev, pager, next, jumper"
            :total="currentPage.total"
        ></el-pagination>
      </div>
    <EtypeEditor :flag="flag" />
  </div>
</template>

<script>
import api from '../../../api'
import qs from 'qs'
import EtypeEditor from './EtypeEditor.vue'
import EtypeHeader from './EtypeHeader.vue'

export default {
  data() {
    return {
      tunnelInfo: [],
      flag: false,
      options: [{ value: '技术员' }, { value: '项目负责人' }, { value: '超前监测' }, { value: '监理人' }],
      value: '--全部--',
      currentPage: {
        pageNo: 1,
        total: 0
      }
    }
  },
  components: {
    EtypeEditor,
    EtypeHeader
  },

  mounted() {
    this.http(1)
    this.$bus.$on('onRefreshEtype', (flag) => {
      this.http(1)
    })
  },
  methods: {
    http(page) {
      api
        .eTypefindByAllpage({ page })
        .then((res) => {
          console.log(res)
          if (res.data.code == 2001) {
            this.tunnelInfo = res.data.data.records
          }
        })
        .catch((error) => {
          console.log(error)
        })
    },
    handleEdit(index, row) {
      this.$bus.$emit('onShowEditor', row)
    },
    handleDelete(index, row) {
      let data = {
        id: row.eId
      }
      this.$confirm('此操作将永久删除开挖方式, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        customClass: 'del-model',
        type: 'warning'
      })
        .then(() => {
          api
            .etypedeleteById(qs.stringify(data))
            .then((res) => {
              if (res.data.code == 2001) {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
                // 视图重绘
                this.http(1)
              } else {
                this.$message({
                  type: 'fail',
                  message: '删除失败!'
                })
              }
            })
            .catch((err) => {
              console.log(err)
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleCurrentChange(val) {
      this.http(val)
    }
  }
}
</script>

<style scoped>
.el-table{
  margin-top: 20px;
}

</style>